# 邓安琪
# 爬取一页的公司核心数据练习

import time
from selenium import webdriver

wb = webdriver.Chrome()

# 打开网页
wb.get("https://quote.eastmoney.com/center/gridlist.html#hs_a_board")
# 等待页面加载完成
wb.implicitly_wait(10)
# 找到股票代码的元素，并点击
# mainpage = wb.find_element("xpath", '//*[@id="table_wrapper-table"]/tbody/tr[1]/td[3]/a').click()

# 获取所有股票链接的元素
pages = wb.find_elements("xpath", '//*[@id="table_wrapper-table"]/tbody/tr/td[3]/a')
# 遍历所有元素
for page in pages:
    # 点击元素，打开新窗口
    page.click()
    time.sleep(1)  # 等待新窗口加载完成
    # 切换到新的窗口
    wb.switch_to.window(wb.window_handles[-1])

    def process_datas(wb):
        main = wb.find_elements("xpath", '//*[@id="app"]/div/div/div[11]/div[1]/div[3]/div[2]/div')
        for datas in main:
            # 提取元素的路径，把最后一个元素去掉
            datas = wb.find_elements("xpath", '//*[@class="quotecore"]/table/tbody/tr/td')[:-1]
            for data in datas:
                # 获取元素的文本内容
                keywords = data.text
                # 将文本内容按空格分割成一个列表
                data = keywords.split(' ')
                print(data)
        main = wb.find_elements("xpath", '//*[@id="app"]/div/div/div[10]/div[1]/div[2]/div[2]/div')
        for datas in main:
            datas = wb.find_elements("xpath", '//*[@class="quotecore"]/table/tbody/tr/td')[:-1]
            for data in datas:
                keywords = data.text
                data = keywords.split(' ')
                print(data)
        main = wb.find_elements("xpath", '//*[@id="app"]/div/div/div[10]/div[1]/div[3]/div[2]/div')
        for datas in main:
            datas = wb.find_elements("xpath", '//*[@class="quotecore"]/table/tbody/tr/td')[:-1]
            for data in datas:
                keywords = data.text
                data = keywords.split(' ')
                print(data)
    # 调用函数
    process_datas(wb)
    # 关闭当前窗口
    wb.close()
    # 切换回主窗口
    wb.switch_to.window(wb.window_handles[0])

# 关闭网页
wb.quit()
